import { ref,onBeforeUnmount,onMounted, Ref } from 'vue'
import ApiAuth from '../../api/ApiAuth';
import CookieUtil from '../../utils/CookieUtil';
import StrUtil from '../../utils/StrUtil';
import System from '../../utils/System';

export default class VueLoginMain {

    timer:Ref<number> = ref(0)
    btnloading:Ref<boolean> = ref(false)
    account:Ref<string> = ref("123456")
    password:Ref<string> = ref("123456")
    
    constructor(){
        onMounted(()=>{
            document.onkeydown = (e)=>{
                if(e.keyCode == 13){
                    this.login()
                }
            }
        })


        onBeforeUnmount(()=>{
            clearInterval(this.timer.value)
            document.onkeydown = (e)=>{
            }
        });
    }

    async login(){
        this.btnloading.value =  true
        await ApiAuth.loginPassWord(this.account.value,this.password.value).then((data:any)=>{
            this.btnloading.value = false
            StrUtil.sr("登录成功");
            let tokenT = data.data.token
            CookieUtil.setToken(tokenT.access,tokenT.expire)
            System.$router.push("/tree")
        }).catch((e:any)=>{
            this.btnloading.value = false
            StrUtil.er("账号或密码错误");
            console.log(e);
        })
    }

}